package com.lzxm.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;




import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.lzxm.constant.DateUtil;
import com.lzxm.constant.ServerResponse;
import com.lzxm.po.User;
import com.lzxm.po.UserInfo;
import com.lzxm.service.UserInfoService;

@Controller
public class UserInfoController {
	
	@Autowired
	private UserInfoService userinfoService;
	
	
	@RequestMapping("/getuserinfo.do")
	@ResponseBody
	public ServerResponse findUserByResultMap(HttpServletRequest request){
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		int user_id=user.getUser_id();
		UserInfo userinfo = userinfoService.getUserInfo(user_id);
		String birth = DateUtil.dateToStr(userinfo.getBirthday());
		userinfo.setBirth(birth);

		ServerResponse response = ServerResponse.createBySuccessData(userinfo);
		return response;
	}
	

	
	@RequestMapping("/updataUserInfo.do")
	public ServerResponse updataUserInfo(HttpServletRequest request){
		ServerResponse response;
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		
		int user_id=user.getUser_id();
		
		String username = request.getParameter("username");
		String sex = request.getParameter("sex");
		int age = Integer.parseInt(request.getParameter("age"));
		
		String phone_number = request.getParameter("phone_number");
		String birthday=request.getParameter("birthday");
		
		
		UserInfo userinfo=new UserInfo();
		userinfo.setUsername(username);
		userinfo.setSex(sex);
		userinfo.setAge(age);
		userinfo.setPhone_number(phone_number);
		userinfo.setBirthday(DateUtil.strToDate(birthday));
		
		userinfoService.updataUserInfo(userinfo);
		
		response = ServerResponse.createBySuccess();
		
		return response;
	}
	
	
	@RequestMapping("/getall/userinfo.do")
	@ResponseBody
	public ServerResponse findAllUser(HttpServletRequest request){
		ServerResponse response;
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		if(user == null){
			response = ServerResponse.createByErrorMsg("请先登陆！");
			return response;
		}
		
		String name = request.getParameter("username");
		List<UserInfo> lists = userinfoService.selectUserInfoByName(name);

		response = ServerResponse.createBySuccessData(lists);
		return response;
	}

}
